'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller RoleController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class RoleController extends Controller {
  async list() {
    /**
        * @summary role列表分页。
        * @description role列表分页。
        * @router get /rolelist
        * @request query integer limit
        * @request query integer page
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.role.list(params);
    this.ctx.body = {
      data: result.rows,
      msg: '',
      count: result.count,
    };
  }
  async listAll() {
    /**
        * @summary role列表。
        * @description role列表。
        * @router get /rolelistall
        */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.role.listAll(tid);
    this.ctx.body = {
      data: result,
    };
  }
  async create() {
    /**
    * @summary rolecreate
    * @description rolecreate
    * @router post /rolecreate
    * @request body rolecreate value 传入参数
    */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.role.create(params);
    global.log.desc = `role:${params.roleName} rolecode:${params.roleCode} role create`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async delete() {
    /**
    * @summary roledelete
    * @description roledelete
    * @router get /roledelete
    * @request query string id
    */
    const params = this.ctx.query;
    const roleResult = await this.ctx.modelmysql.RoleInfo.findOne({ where: { id: params.id } });
    const result = await this.ctx.service.role.delete(params);
    global.log.desc = `role:${roleResult.name} rolecode:${roleResult.RoleCode} role delete`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async edit() {
    /**
    * @summary roleedit
    * @description roleedit
    * @router post /roleedit
    * @request body roleedit value 传入参数
    */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.role.edit(params);
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async single() {
    /**
    * @summary rolesingle
    * @description rolesingle
    * @router get /rolesingle
    * @request query string id
    */
    const params = this.ctx.query;
    const result = await this.ctx.service.role.single(params);
    this.ctx.body = {
      data: result,
    };
  }
  async roleInfoUpdateMenus() {
    const params = this.ctx.request.body;
    const result = await this.ctx.service.role.roleInfoUpdateMenus(params);
    const roleResult = await this.ctx.modelmysql.RoleInfo.findOne({ where: { id: params.id } });
    global.log.desc = `${roleResult.name} edit menus`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async roleuser() {
    /**
    * @summary roleuser
    * @description roleuser
    * @router post /roleuser
    * @request body roleuser value 传入参数
    */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.role.roleuser(params);
    this.ctx.body = {
      data: result,
    };
  }
}

module.exports = RoleController;
